import axios from "axios";

// 创建 Axios 实例
const instance = axios.create({
    baseURL: '/api',
    timeout: 10000,
    headers: {
        'X-Custom-Header': 'foobar',
        'Content-Type': 'application/json'
    }
});

// 用户相关API
// 根据后端实际API路径调整
export const getUsers = async () => {
  try {
    const response = await instance.get('/findUser');
      console.log('接口返回数据:', response.data);
    return response.data;
  } catch (error) {
    console.error('查找用户失败:', error);
    throw error;
  }
};

// 根据后端实际API路径调整
export const insertUser = async (userData) => {
    try {
        await instance({
            url: '/insertUser',
            method: 'post',
            data: userData});
    }
    catch (error) {
      console.error('插入失败:', error);
      throw error;
    }
  };
export const insertUserInfo = async (userData) => {
    try {
        await instance({
            url: '/insertUserInfo',
            method: 'post',
            data: userData});
    }
    catch (error) {
        console.error('插入失败:', error);
        throw error;
    }
};

export const getUserInfo = async (id) => {
    try {
        const response = await instance.post('/userInfo', id );
        console.log(response.data)
        return response.data;
    } catch (error) {
        console.error('获取用户信息失败:', error);
        throw error;
    }
};

export const updateUserInfo = async (userData) => {
    try {
        await instance.post(
            '/updateUserInfo', userData);

    } catch (error) {
        console.error('更新用户信息失败:', error);
        throw error;
    }
};

export const updateUser = async (userData) => {
    try {
        await instance.post(
            '/updateUser', userData);
    } catch (error) {
        console.error('更新用户信息失败:', error);
        throw error;
    }
};

export const deleteUser = async (userData) => {
    try {
        await instance.post(
            '/deleteUser', userData);
    } catch (error) {
        console.error('更新用户信息失败:', error);
        throw error;
    }
};
export const deleteUserByIds = async ( ids) => {
    try {
        await instance.post(
            '/deleteUserByIds', ids);
    } catch (error) {
        console.error('更新用户信息失败:', error);
        throw error;
    }
};

export const changePassword = async (userData) => {
    try {
         await instance.post('/updateUser', userData);
        return '修改密码成功'
    } catch (error) {   Q1W
        console.error('修改密码失败:', error);
        throw error;
    }
};



// // 请求拦截器 - 添加认证令牌
// instance.interceptors.request.use(config => {
//     const token = localStorage.getItem('token') || getToken(); // 假设您有 getToken 方法
//     if (token) {
//         config.headers['Authorization'] = `Bearer ${token}`;
//     }
//     return config;
// }, error => {
//     return Promise.reject(error);
// });

/**
 * 文件上传 API（适配最新后端）
 * @param {File} file - 要上传的文件
 * @param {string} type - 文件类型/分类 (avatar, document, etc.)
 * @param {Object} [extraParams={}] - 额外参数
 * @returns {Promise} Axios 响应对象
 */
// 文件上传函数
export const uploadImage = (file) => {
    const formData = new FormData();
    formData.append('file', file);

    return axios.post('/api/uploadImage', formData, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    });
};


export function useUserStore() {
}


export default instance;
